<template>
    <app-wrap ref="wrap" @login-success="loginSuccessHandle">
        <card-detail v-if="cardInfo" ref="card" :card-info="cardInfo" :show-share-ctrl="showShareCtrl"></card-detail>
        <div class="no-card" v-if="!cardInfo">
            <div v-if="!!$mp.query.id" class="notFound">
                <div class="text">
                    <img src="../static/images/no-card.png" mode="widthFix" alt="">
                    <div class="text">
                        <text>出错啦！</text>
                        <text>卡片不存在</text>
                    </div>
                </div>
                <van-button round size="large" type="primary" @click="myCard">返回我的卡片</van-button>
            </div>
            <template v-else>
                <img src="../static/images/no-card.png" mode="widthFix" alt="">
                <div class="text">
                    <text>你还没有创建名片</text>
                    <text>快去创建一个吧</text>
                </div>
                <van-button round size="large" type="primary" @click="createCard">创建名片</van-button>
            </template>
        </div>
    </app-wrap>
</template>

<config>
    {
    "navigationBarTitleText": "安利名片+"
    }
</config>

<script>
  import appWrap from '@/components/app-wrap'
  import cardDetail from '@/components/card-detail'

  import {mapGetters} from "vuex";
  import {getCard, getCheckPrivacy, getUpdatePrivacy, getUpdateunionid, memberAuthStep2 } from "@/api";
  import qs from "qs";

  export default {
    name: 'index',
    components: {
      appWrap, cardDetail
    },
    data() {
      return {
        cardInfo: null,
        loading: false,
        showShareCtrl: true,
      }
    },
    computed: {
      ...mapGetters(['userInfo']),
      cardID(){
        const scene = qs.parse(decodeURIComponent(this.$mp.query.scene));
        return this.$mp.query.id || scene.id;
      }
    },
    methods: {
      // 登录成功后获取本人卡片信息，确保本人已经登录
      loginSuccessHandle() {
        this.$appWrap.showLoading();
        this.loading = true;
        console.log('-----------appwrap--------')
        let self = this
        this.$nextTick(function(){
          console.log(this.$refs)
          console.log(this.$refs.wrap)
          let token = self.$store.getters.token
          console.log('----id--')
          console.log(self.$store.getters.userInfo)
          const {unionId, id} = self.$store.getters.userInfo

          let pars = {
            unionId, 
            id
          }
          // 用户的unionID
          getUpdateunionid(pars).then( r2 => {
            console.log('---r2r2r2---')
            console.log(r2)
          }) 
          memberAuthStep2(self.$store.getters.userInfo).then( res => {
            console.log('----memberAuthStep2---')
          })
          // check是否更新了
          getCheckPrivacy(id).then( r => {
            console.log('rrrrrrrrrrrrrr--getCheckPrivacy--')
            console.log(r)
            if(r.code == 200 && r.data != 1){
              // getUpdatePrivacy(id).then( r => {
              //   console.log('rrrrrrrrrrrrrr--getUpdatePrivacy--')
              //   console.log(r)
              // })
              this.$refs.wrap.showProtocol()
            }

          })
          // this.$refs.wrap.showProtocol()
        })



        if(this.cardID){
          this.$nextTick(() => {
            setTimeout(()=>{
              if(this.userInfo && this.userInfo.unionId && this.$refs.card && this.$refs.card.apiLog){
                this.$refs.card.apiLog();
              }
              this.$appWrap.hideLoading();
            },1000)
          })
        }else{
          if(this.userInfo.role!== 'ABO'){
            Megalo.redirectTo({
              url: '/pages/no-abo'
            })
          }else{
            getCard('')
              .then(data => {
                console.log('----getCard---')
                this.loading = false;
                this.$appWrap.hideLoading();
                if (data && data.data) {
                  this.cardInfo = data.data;
                  this.$store.dispatch('updateCardInfo', data.data);
                }else{
                  this.cardInfo = null;
                }
              })
          }
        }

      },
      createCard() {
        Megalo.navigateTo({
          url: '/pages/add-card'
        })
      },
      myCard() {
        Megalo.redirectTo({
          url: '/pages/index'
        })
      },
    },
    mounted(){
      // this.$store.dispatch('updateCardInfo');


      if(this.cardID){
        getCard(this.cardID)
          .then(data => {
            this.loading = false;
            this.$appWrap.hideLoading();
            if (data.data) {
              this.cardInfo = data.data;
              this.$store.dispatch('updateCardInfo', data.data);
            }else{
              this.cardInfo = null;
            }
          })
      }
    },
    onShareAppMessage(){
      if(!this.cardInfo) return;

      return {
        title: this.cardInfo.name + ' 的名片',
        path: '/pages/index?id=' + this.cardInfo.id,
        // imageUrl: this.$refs.card.cardPath
      }
    },
  }
</script>

<style lang="less">
    .content {
        padding: 36px;
    }

    .intro {
        font-size: 28px;
        font-weight: 400;
        line-height: 42px;
        color: rgba(150, 156, 161, 1);
        padding-bottom: 54px;
        margin-bottom: 54px;
        border-bottom: 1px solid rgba(226, 226, 226, 1);
    }

    .recent-visitors {
        margin-bottom: 30px;
    }

    .notFound {
        /*padding-top: 20vh;*/

        text {
            margin-bottom: 2vh;
            font-size: 36px;
        }
    }

    .my-card-btn {
        width: 169px;
        height: 60px;
        background: rgba(0, 47, 95, 1);
        box-shadow: 0 4px 10px rgba(0, 47, 95, 0.2);
        border-radius: 80px 0 0 80px;
        font-size: 28px;
        font-weight: 400;
        line-height: 60px;
        color: rgba(255, 255, 255, 1);
        text-align: center;
        position: fixed;
        top: 177px;
        right: 0;
        animation: fadeRight 500ms 500ms both;
        opacity: 0;
        transform: translateX(100%);
    }

    @keyframes fadeRight {
        to {
            opacity: 1;
            transform: none;
        }
    }

    .no-card {
        width: 100vw;
        overflow: hidden;
        height: 100vh;
        background-color: #F8F8F9;
        padding-left: 36px;
        padding-right: 36px;
        box-sizing: border-box;

        .text {
            margin-bottom: 70px;

            text {
                display: block;
                text-align: center;

                &:first-child {
                    font-size: 36px;
                    font-weight: 500;
                    color: rgba(0, 0, 0, 1);
                }

                &:last-child {
                    font-size: 30px;
                    font-weight: 400;
                    color: rgba(150, 156, 161, 1);
                }
            }
        }

        img {
            width: 363px;
            display: block;
            margin: 61px auto auto 160px;
        }

        .van-button--danger {
            background-color: #ED174B;
            border-color: #ED174B;
        }
    }
</style>
